﻿/*
 * 商品信息管理
 * 
 * 
 * 2011/11/05
 * 1. 修改绑定数据在恰好分页时,最后一页数据Index报错的BUG
 * 2. 增加翻页时判断.
 * 3. 增加直接输入数据进行跳指定页面
 * 4. 增加搜索完成后清除搜索结果的按钮.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EShop.BLL;
using EShop.Model;
using EShop.Common;

namespace EShop.Client
{
    public partial class frmMerchandiseBasicManger : frmBase
    {
        public frmMerchandiseBasicManger()
        {
            InitializeComponent();
        }


        private int pageSize = oShopConfigInfo.PageSize;     //每页显示行数  
        private int RowCount = 0;         //总记录数  
        private int iLastPageRowCount = 0;//最后一页记录数
        private int pageCount = 0;    //总页数＝总记录数/每页显示行数  
        private int pageCurrent = 0;   //当前页号 

        private List<MerchandiseBasicInfo> aMerchandiseBasicInfoList;
        private Merchandise_BasicInfoBLL oBaseInfoBLL = new Merchandise_BasicInfoBLL();
        private void MerchandiseBasicManger_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void Init()
        {
            try
            {

                List<MerchandiseClass> aMerchandiseClassList = new MerchandiseClassBLL().GetAllMerchandiseClassList();
                foreach (MerchandiseClass merchandiseClassItem in aMerchandiseClassList)
                {
                    cmb_MerchandiseType.Items.Add(merchandiseClassItem);
                }

                aMerchandiseBasicInfoList = oBaseInfoBLL.GetAllMerchandiseBasicInfo();
                initPageInfo();
                bindData();
            }
            catch (Exception ex)
            {
                LogWrite(ex);
            }
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void initPageInfo()
        {
            RowCount = aMerchandiseBasicInfoList.Count;
            iLastPageRowCount = RowCount % pageSize;
            //pageCount = RowCount / pageSize + (iLastPageRowCount > 0 ? 1 : 0);
            pageCount = RowCount / pageSize;
            if (RowCount % pageSize != 0)
            {
                pageCount += 1;
                iLastPageRowCount = RowCount % pageSize;
            }
            else
            {
                iLastPageRowCount = pageSize;
            }
            if (pageCurrent > pageCount - 1)
                pageCurrent = 0;
            lblPageCount.Text = "/" + pageCount.ToString();
        }

        private void bindData()
        {
            if (RowCount == 0)
            {
                MoveFirstPage.Enabled = false;
                MovePreviousPage.Enabled = false;
                MoveNextPage.Enabled = false;
                MoveLastPage.Enabled = false;
                return;
            }

            int iStart = 0, iSelectRowCount = 0;

            //判断是否是最后一页
            bool blIsLastPage = pageCurrent == (pageCount - 1) ? true : false;
            if (blIsLastPage)
            {
                //if (iLastPageRowCount == 0)//整除
                //    iSelectRowCount = RowCount;
                //else
                iSelectRowCount = iLastPageRowCount;
            }
            else
                iSelectRowCount = pageSize;

            iStart = pageCurrent*pageSize;
            bdsInfo.DataSource = aMerchandiseBasicInfoList.GetRange(iStart, iSelectRowCount);
            //bdsInfo.DataSource=aMerchandiseBasicInfoList.GetRange(0,
            //设置按钮
            lblCurrentPage.Text = (pageCurrent + 1).ToString();
            MoveFirstPage.Enabled = iStart == 0 ? false : true;
            MovePreviousPage.Enabled = iStart == 0 ? false : true;
            MoveNextPage.Enabled = blIsLastPage ? false : true;
            MoveLastPage.Enabled = blIsLastPage ? false : true;
        }

        private void MoveFirstPage_Click(object sender, EventArgs e)
        {
            if (pageCurrent == 0) return;
            pageCurrent = 0;
            bindData();
        }

        private void MovePreviousPage_Click(object sender, EventArgs e)
        {
            if (pageCurrent == 0) return;
            pageCurrent--;
            bindData();
        }

        private void MoveNextPage_Click(object sender, EventArgs e)
        {
            if (pageCurrent == pageCount - 1) return;
            pageCurrent++;
            bindData();
        }

        private void MoveLastPage_Click(object sender, EventArgs e)
        {
            if (pageCurrent >= pageCount - 1) return;
            pageCurrent = pageCount-1;
            bindData();
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            using (frmMerchandiseBasicInfoEdit editForm = new frmMerchandiseBasicInfoEdit())
            {
                editForm.ShowDialog();
                if(editForm.DialogResult==DialogResult.OK)
                {
                    aMerchandiseBasicInfoList.Add(editForm.MerchandiseBasicInfo);
                    initPageInfo();
                    bindData();
                }
            }
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("您真的要删除商品吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if(dialogResult==DialogResult.Yes)
            {
                string strBarCode = gvw_MerchandiseBasicInfoList.SelectedRows[0].Cells[0].Value.ToString();
                if(oBaseInfoBLL.Delete(strBarCode))
                {
                    //TODO: Test
                    MerchandiseBasicInfo oInfo = aMerchandiseBasicInfoList.Find(delegate(MerchandiseBasicInfo info)
                                                                            { return info.Barcode == strBarCode; });
                    if (oInfo != null)
                        aMerchandiseBasicInfoList.Remove(oInfo);
                    initPageInfo();
                    bindData();
                }
            }
        }

        private void EditItem_Click(object sender, EventArgs e)
        {
            try
            {
                string strBarCode = gvw_MerchandiseBasicInfoList.SelectedRows[0].Cells[0].Value.ToString();
                MerchandiseBasicInfo oInfo = aMerchandiseBasicInfoList.Find(delegate(MerchandiseBasicInfo info)
                                                                                { return info.Barcode == strBarCode; });
                if (oInfo != null)
                {
                    using (frmMerchandiseBasicInfoEdit editForm = new frmMerchandiseBasicInfoEdit())
                    {
                        editForm.MerchandiseBasicInfo = oInfo;
                        editForm.ShowDialog();
                        if (editForm.DialogResult == DialogResult.OK)
                        {
                            aMerchandiseBasicInfoList.Remove(oInfo);
                            aMerchandiseBasicInfoList.Add(editForm.MerchandiseBasicInfo);
                            initPageInfo();
                            bindData();
                        }
                    }
                }
            }
            catch { }
        }

        private void tlsbtnSearch_Click(object sender, EventArgs e)
        {
            string strSearchVale = SystemLibrary.SQLQuoteName(txtMerchandiseName.Text.Trim());
            int iClassID = 0;
            if (cmb_MerchandiseType.SelectedItem != null)
                iClassID = ((MerchandiseClass)cmb_MerchandiseType.SelectedItem).MerchandiseClassID;

            if (string.IsNullOrEmpty(strSearchVale) && iClassID == 0)
                aMerchandiseBasicInfoList = oBaseInfoBLL.GetAllMerchandiseBasicInfo();
            else
                aMerchandiseBasicInfoList = oBaseInfoBLL.SearchMerchandiseBasicInfo(strSearchVale, iClassID);

            initPageInfo();
            bindData();
        }

        private void frmMerchandiseBasicManger_FormClosing(object sender, FormClosingEventArgs e)
        {
            //if (this.DialogResult == DialogResult.Yes)
                gvw_MerchandiseBasicInfoList.Dispose();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            Init();
            this.cmb_MerchandiseType.SelectedIndex = -1;
        }

        private void lblCurrentPage_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                int iPage = Convert.ToInt32(lblCurrentPage.Text) - 1;
                if (iPage == pageCurrent) return;
                if (iPage > pageCount || iPage < 0)
                {
                    lblCurrentPage.Text = (pageCurrent + 1).ToString();
                    return;
                }
                pageCurrent = iPage;
                bindData();
            }
            else
            {
                
                
            }
        }
        private void lblCurrentPage_KeyDown(object sender, KeyEventArgs e)
        {
            string tmp = lblCurrentPage.Text;
            ////if(e.KeyCode=Keys.ent
        }

        private void lblCurrentPage_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int i = int.Parse(lblCurrentPage.Text);
            }
            catch
            {
                lblCurrentPage.Text = (pageCurrent + 1).ToString();
            }
        }
    }
}
